package com.yuwei.play.fragment

import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.yuwei.base.RoutePath
import com.yuwei.database.entity.MovieEntity
import com.yuwei.common.mvi.BaseMviFragment
import com.yuwei.common.mvi.UiState
import com.yuwei.play.adapter.PlayListAdapter
import com.yuwei.play.databinding.FragmentProfileBinding
import com.yuwei.play.mvi.PlayIntent
import com.yuwei.play.mvi.PlayViewModel



class ProfileFragment : BaseMviFragment<FragmentProfileBinding,PlayViewModel>() {
    private val playListAdapter = PlayListAdapter()
    private var mPosition = -1
    override fun successData(it: UiState.onSuccess<*>) {
        when (it.type) {
            "getMovieRecommend" -> {
                val data = it.data as MovieEntity
                playListAdapter.data.addAll(data)
                playListAdapter.notifyDataSetChanged()

            }
        }
    }

    override fun failureData(error: String) {

    }

    override fun initView() {
        val string = SPUtils.getInstance().getString("title")
        if (string!=null){
            binding.playerProText.text = string
        }
        binding.playerRv.adapter = playListAdapter
        binding.playerRv.layoutManager = LinearLayoutManager(requireContext())
        vm.sendIntent(PlayIntent.getVideoRecommend(1,10))

        playListAdapter.setOnItemClickListener { adapter, view, position ->
            mPosition = position
            SPUtils.getInstance().put("index",mPosition)
            playListAdapter.sendPosition(mPosition)
            playListAdapter.notifyDataSetChanged()
            ARouter.getInstance().build(RoutePath.VIDEO_PLAY_DETAIL).withSerializable("movie", playListAdapter.data[position]).navigation()
            activity?.finish()
        }
        val int = SPUtils.getInstance().getInt("index")
        if (int != null){
            playListAdapter.sendPosition(int)
            playListAdapter.notifyDataSetChanged()
        }



    }

    override fun initData() {

    }

    override fun onDestroy() {
        super.onDestroy()
        SPUtils.getInstance().clear()
    }

}